package org.ww.edu.controller.front;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.databind.util.BeanUtil;
import net.bytebuddy.asm.Advice;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.ww.commonutils.JwtUtils;
import org.ww.commonutils.R;
import org.ww.commonutils.ordervo.CourseWebVoOrder;
import org.ww.edu.bean.Course;
import org.ww.edu.bean.frontvo.CourseWebVo;
import org.ww.edu.bean.frontvo.FrontCourseVo;
import org.ww.edu.bean.vo.ChapterVo;
import org.ww.edu.client.OrderClient;
import org.ww.edu.service.ChapterService;
import org.ww.edu.service.CourseService;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/edu/coursefront")
//@CrossOrigin
public class CourseFrontController {

    @Autowired
    private CourseService courseService;
    @Autowired
    private ChapterService chapterService;
    @Autowired
    private OrderClient orderClient;

    /**
     * 分页带条件查询课程信息
     * @param page
     * @param limit
     * @param courseVo
     * @return
     */
    @PostMapping("/pageCourseInfo/{page}/{limit}")
    public R pageCourseInfo(@PathVariable long page, @PathVariable long limit,
                            @RequestBody FrontCourseVo courseVo){
        Page<Course> coursePage=new Page<>(page,limit);
        Map<String,Object> map=courseService.pageCourseInfo(coursePage,courseVo);
        return R.ok().data(map);
    }

    /**
     * 根据课程id查询课程详细信息
     * @param courseId
     * @return
     */
    @GetMapping("/getBaseCourseInfo/{courseId}")
    public R getBaseCourseInfo(@PathVariable String courseId, HttpServletRequest request){
        //1 根据课程id查询封装课程的基本信息
        CourseWebVo courseWebVo=courseService.getBaseCourseInfo(courseId);
        //2 根据课程id查询章节小节信息
        List<ChapterVo> allChapterVideo = chapterService.getAllChapterVideo(courseId);
        //3 根据用户id和课程id查看该课程是否被购买
        boolean isBuy = orderClient.isBuyCourse(courseId, JwtUtils.getMemberIdByJwtToken(request));
        return R.ok().data("courseWebVo",courseWebVo).data("allChapterVideo",allChapterVideo).data("isBuy",isBuy);
    }

    /**
     * 根据课程id获取课程信息-->订单模块调用
     * @param id
     * @return
     */
    @GetMapping("/getCourseInfoOrder/{id}")
    public CourseWebVoOrder getCourseInfoOrder(@PathVariable String id){
        Course course = courseService.getById(id);
        CourseWebVoOrder courseWebVoOrder=new CourseWebVoOrder();
        BeanUtils.copyProperties(course,courseWebVoOrder);
        return courseWebVoOrder;
    }

}
